package com.warning_speed_android.gps;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {

	private final Context context;
	boolean isGpsEnable = false;
	boolean canGetLocation = false;
	boolean isNetworkEnable = false;
	double lat, lon;
	Location location;
	private static final long LOCATION_REFRESH_TIME = 1000;// 1s
	private static final float LOCATION_REFRESH_DISTANCE = 10;// 10met
	private LocationManager locationManager;

	public GPSTracker(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		getLocation();
	}

	public Location getLocation() {
		try {
			locationManager = (LocationManager) context
					.getSystemService(LOCATION_SERVICE);
			isGpsEnable = locationManager
					.isProviderEnabled(LocationManager.GPS_PROVIDER);
			isNetworkEnable = locationManager
					.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			if (!isGpsEnable && !isNetworkEnable) {

			} else {
				canGetLocation = true;
				if (isNetworkEnable) {
					locationManager.requestLocationUpdates(
							LocationManager.NETWORK_PROVIDER,
							LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE,
							this);
				}
				if(locationManager != null){
					location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
					if(location != null){
						lat = location.getLatitude();
						lon = location.getLongitude();
					}
				}
				
				if(isGpsEnable){
					if(location == null){
						locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, this);
						if(locationManager != null){
							location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if(location != null){
								lat = location.getLatitude();
								lon = location.getLongitude();
							}
							
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			Log.e(GPSTracker.class.getSimpleName(), e.getMessage(), e);
		}
		return location;
	}
	
	public void stopUsingGPS(){
		if(locationManager != null){
			locationManager.removeUpdates(GPSTracker.this);
		}
	}
	

	public double getLat() {
		if(location != null){
			lat = location.getLatitude();
		}
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLon() {
		if(location != null){
			lon = location.getLongitude();
		}
		return lon;
	}

	public void setLon(double lon) {
		this.lon = lon;
	}
	
	public void showSettingAlert(){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("GPS is setting.");
		builder.setMessage("GPS is not enable. Do you want to go to seetings menu?");
		builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				context.startActivity(intent);
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		builder.show();
	}
	
	

	public boolean isGpsEnable() {
		return isGpsEnable;
	}

	public void setGpsEnable(boolean isGpsEnable) {
		this.isGpsEnable = isGpsEnable;
	}

	

	public boolean isCanGetLocation() {
		return canGetLocation;
	}

	public void setCanGetLocation(boolean canGetLocation) {
		this.canGetLocation = canGetLocation;
	}

	public boolean isNetworkEnable() {
		return isNetworkEnable;
	}

	public void setNetworkEnable(boolean isNetworkEnable) {
		this.isNetworkEnable = isNetworkEnable;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
